// LeftTree.cpp : implementation file
//

#include "stdafx.h"
#include "DAD.h"
#include "LeftTree.h"
#include "MyObject1.h"

#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1
#include <stdexcept>
#include <fstream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;


#include "header/dbciterator.hpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLeftTree

IMPLEMENT_DYNCREATE(CLeftTree, CTreeView)

CLeftTree::CLeftTree()
{
}

CLeftTree::~CLeftTree()
{
}


BEGIN_MESSAGE_MAP(CLeftTree, CTreeView)
	//{{AFX_MSG_MAP(CLeftTree)
	ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBegindrag)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeftTree drawing

void CLeftTree::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CLeftTree diagnostics

#ifdef _DEBUG
void CLeftTree::AssertValid() const
{
	CTreeView::AssertValid();
}

void CLeftTree::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeftTree message handlers

BOOL CLeftTree::PreCreateWindow(CREATESTRUCT& cs) 
{
	//cs.style |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS  ;
	cs.style |= WS_VISIBLE | LVS_REPORT | TVS_HASLINES | TVS_LINESATROOT | TVS_CHECKBOXES;

	return CTreeView::PreCreateWindow(cs);
}

void CLeftTree::OnInitialUpdate() 
{
	CTreeView::OnInitialUpdate();
	
	HTREEITEM LastRoot;
	HTREEITEM CurrentItem;

	CTreeCtrl* Tree = &GetTreeCtrl();

	DBCIterator dbc("F:\\sxm\\can\\meet_egrv.dbc");

	for (auto message : dbc) {
		LastRoot = Tree->InsertItem(message.getName().c_str(), TVI_ROOT, TVI_LAST);
		
		for (auto sig : message) {
			CurrentItem = Tree->InsertItem(sig.getName().c_str(), LastRoot, TVI_LAST);
		}
	}
}

void CLeftTree::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM    hItem = pNMTreeView->itemNew.hItem;
	if (!hItem) return;
	GetTreeCtrl().SelectItem(hItem);
}

void CLeftTree::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTreeView::OnLButtonDown(nFlags, point);
	CMyObject1* MyObj= new CMyObject1();

	CTreeCtrl* Tree = &GetTreeCtrl();

	HTREEITEM curItem = Tree->GetSelectedItem();

	CString Text = Tree->GetItemText(curItem);

	/*UINT state = Tree->GetItemState(curItem, TVIS_SELECTED);

	if (state & TVIS_SELECTED)
	{
		

		int i = 0;

	}*/
	
	if (Text.Compare("egrv_cmd_normal") == 0 || Text.Compare("egrv_status_normal") == 0) {
		return;
	}

	MyObj->Data = Text;

	RECT rClient;
    GetClientRect(&rClient);

	m_DropSource.StartDragging((DWORD)MyObj, &rClient, &point);

}
